﻿using Mangdot.Webchat.Common;
using Mangdot.Webchat.Models;
using System;
using System.Linq;
using System.Web.Mvc;

namespace Mangdot.Webchat.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            //string result = Common.WebchatService.CreateMenu();
            //ViewBag.Menu = result;

            var user = Session["UserId"];
            if (user == null)
            {
                return RedirectToAction("Login","WeChat");
            }
            Logger.Info(user.ToString());

            string url = Request.Url.AbsoluteUri;
            var config = WebchatService.GetWXConfig(url);

            ViewBag.appId = config.appId;
            ViewBag.nonceStr = config.nonceStr;
            ViewBag.signature = config.signature;
            ViewBag.timestamp = config.timestamp;



            return View();
        }

        public ActionResult ViewHK(Guid id)
        {
            using (var context = new Data.HeKaEntities())
            {
                var query = (from item in context.UserHK
                             join t in context.Themes on item.ThemeId equals t.Id
                             where item.Id == id
                             select new HKModel
                             {
                                 Id = item.Id.ToString(),
                                 ThemeId = item.ThemeId,
                                 ScrollTxt = item.HKTxt,
                                 BackgroundImg = item.BackgroudImage,
                                 BackgroundMusic = item.MusicUrl

                             }).FirstOrDefault();

                return View(query);
            }
        }

        public PartialViewResult View001(HKModel model)
        {
            string url = Request.Url.AbsoluteUri;
            var config = WebchatService.GetWXConfig(url);
            ViewBag.appId = config.appId;
            ViewBag.nonceStr = config.nonceStr;
            ViewBag.signature = config.signature;
            ViewBag.timestamp = config.timestamp;
            return PartialView(model);
        }

        [HttpPost]
        public JsonResult GetTheme()
        {
            using (var context = new Data.HeKaEntities())
            {
                var query = (from item in context.Themes
                             select item).OrderByDescending(x => x.CreateTime).ToList();

                return Json(query);
            }
        }

        [HttpPost]
        public JsonResult AddHK(int themeId, string hkTxt, string musicUrl, string img)
        {
            try
            {
                var id = Guid.NewGuid();
                using (var context = new Data.HeKaEntities())
                {
                    context.UserHK.Add(new Data.UserHK
                    {
                        Id = id,
                        ThemeId = themeId,
                        HKTxt = hkTxt,
                        MusicUrl = musicUrl,
                        BackgroudImage = img
                    });

                    var isOk = context.SaveChanges() > 0;

                    var query = (from item in context.UserHK
                                 join t in context.Themes on item.ThemeId equals t.Id
                                 where item.Id == id
                                 select new HKModel
                                 {
                                     Id = item.Id.ToString(),
                                     ThemePath = t.PageUrl,
                                     ThemeId = item.ThemeId,
                                     ScrollTxt = item.HKTxt,
                                     BackgroundImg = item.BackgroudImage,
                                     BackgroundMusic = item.MusicUrl

                                 }).FirstOrDefault();

                    var htmlUrl = RazorHelper.CompileHtml(query);

                    return Json(new { result = isOk, value = id, url = htmlUrl });
                }
            }
            catch (Exception e)
            {
                return Json(new { result = false });
            }
        }


        public ActionResult test()
        {
            return View();
        }
    }
}